//
//  ActivityWorkoutsRecordsView.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/10/31.
//

import Foundation
import MYModule
import UIKit

class ActivityWorkoutsRecordsView: View {
    
    var workoutsRecords: [TrainingModel] = [] {
        
        didSet {
            
            tableView.reloadData()
            
        }
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        self.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        let stackView = StackView()
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .fillEqually
        stackView.backgroundColor = UIColor.background
        stackView.addArrangedSubview(self.timesView)
        stackView.addArrangedSubview(self.durationView)
        stackView.addArrangedSubview(self.kcalView)
        
        self.addSubview(stackView)
        self.addSubview(self.tableView)
        
        stackView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(25)
            make.leading.trailing.equalToSuperview()
        }
        
        tableView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(stackView.snp.bottom).offset(15)
            make.bottom.equalTo(-20)
        }
        
        self.backgroundColor = UIColor.background
        self.titleLabel.text = "activity_workouts_records_title".language
    }
    
    //MARK: - lazy load -
    public lazy var titleLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_222222
        label.font = Fonts.boldSystemFont(ofSize: 14)
        return label
    }()

    lazy var timesView: ActivityWorkoutsRecordCurrentTotalItem = {
        let view = ActivityWorkoutsRecordCurrentTotalItem()
        view.iconImageView.image = UIImage(named: "ic_home_today_workout_records_times")
        view.titleLabel.text = "activity_total_times".language
        view.numLabel.text = "--"
        view.unitLabel.text = "unit_times".language
        return view
    }()
    
    lazy var durationView: ActivityWorkoutsRecordCurrentTotalItem = {
        let view = ActivityWorkoutsRecordCurrentTotalItem()
        view.iconImageView.image = UIImage(named: "ic_home_today_workout_records_duration")
        view.titleLabel.text = "activity_total_duration".language
        view.numLabel.text = "--"
        view.unitLabel.text = "unit_minute".language
        return view
    }()

    lazy var kcalView: ActivityWorkoutsRecordCurrentTotalItem = {
        let view = ActivityWorkoutsRecordCurrentTotalItem()
        view.iconImageView.image = UIImage(named: "ic_home_today_workout_records_calories")
        view.titleLabel.text = "activity_total_calories".language
        view.numLabel.text = "--"
        view.unitLabel.text = "unit_calorie".language
        return view
    }()
    
    public lazy var tableView: TableView = {
        let tableView = TableView(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.background
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        tableView.separatorStyle = .singleLine
        tableView.separatorColor = UIColor.assist_D0D0D0
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = UITableView.automaticDimension

        tableView.register(WorkoutsRecordsTableViewCell.self, forCellReuseIdentifier: WorkoutsRecordsTableViewCell.reuseIdentifier)
        tableView.automaticallyContentSize = true
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = false
        return tableView
    }()
}

extension ActivityWorkoutsRecordsView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return workoutsRecords.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutsRecordsTableViewCell.reuseIdentifier) as! WorkoutsRecordsTableViewCell
        
        cell.trainingModel = workoutsRecords[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = self.workoutsRecords[indexPath.row]
        
        guard let type = TrainingType(rawValue: model.type) else {
            return
        }
        
        if type.isGPS && !model.locations.isEmpty {
            if let currentVC = UIViewController.currentViewController() {
                if model.locations.count == 0 {
                    let vc = TrainingDetailViewController()
                    vc.showRightItem = false
                    vc.viewModel = TrainingDetailViewModel(trainModel: model)
                    currentVC.navigationController?.pushViewController(vc, animated: true)
                    return
                }
                
                let vc = GPSTrainingRecordTableViewController()
                vc.type = type
                let gpsModel = model
                vc.gpsModel = gpsModel
                currentVC.navigationController?.pushViewController(vc, animated: true)
            }
        } else {
            if let currentVC = UIViewController.currentViewController() {
                let vc = TrainingDetailViewController()
                vc.showRightItem = false
                vc.viewModel = TrainingDetailViewModel(trainModel: model)
                currentVC.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}

